import {  defineConfig, loadEnv, ConfigEnv, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { wrapperEnv } from "./src/utils/getEnv";
import {resolve} from "path";
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'

export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
	const env = loadEnv(mode, process.cwd());
	const viteEnv = wrapperEnv(env);

	return {
		base: viteEnv.VITE_BASE_URL+'/',
		resolve: {
			alias: {
				"@": resolve(__dirname, "./src")
			}
		},
		css: {
			preprocessorOptions: {
				scss: {
					additionalData: `@import "@/styles/common.scss";`
				}
			}
		},
		server: {
			// 服务器主机名，如果允许外部访问，可设置为 "0.0.0.0"
			host: "0.0.0.0",
			port: viteEnv.VITE_PORT,
			open: viteEnv.VITE_OPEN,
			cors: true,
			// 跨域代理配置
			proxy: {
				"/api": {
					// target: "https://mock.mengxuegu.com/mock/629d727e6163854a32e8307e", // easymock
					// target: "https://www.fastmock.site/mock/f81e8333c1a9276214bcdbc170d9e0a0", // fastmock
					target: "http://localhost:8000", // easymock
					changeOrigin: true,
					rewrite: path => path.replace(/^\/api/, "")
				}
			}
		},
		plugins: [
			vue(),
			createSvgIconsPlugin({
				// 指定要缓存的文件夹
				iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
				// 指定symbolId格式
				symbolId: '[name]'
			  })
		],
		// * 打包去除 console.log && debugger
		esbuild: {
			pure: viteEnv.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
		},
		build: {
			outDir: viteEnv.VITE_BASE_URL.substring(1),
			// minify: "esbuild",
			// esbuild 打包更快，但是不能去除 console.log，terser打包慢，但能去除 console.log
			minify: "terser",
			terserOptions: {
				compress: {
					drop_console: viteEnv.VITE_DROP_CONSOLE,
					drop_debugger: true
				}
			},
			chunkSizeWarningLimit: 1500,
			// rollupOptions: {
			// 	output: {
			// 		// Static resource classification and packaging
			// 		chunkFileNames: "assets/js/[name]-[hash].js",
			// 		entryFileNames: "assets/js/[name]-[hash].js",
			// 		assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
			// 	}
			// }
		}
	};
});
